
package drexel.cs544.nap.server.xml;

import org.xml.sax.*;
import org.xml.sax.helpers.*;
import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;

public class SaxDocument
{
  XMLReader parser;
  public XMLReader getParser() { return parser; }
  DefaultHandler handler;
  boolean bParserCompleted = false;
  public void setParserCompleted(boolean bParserCompleted) { this.bParserCompleted = bParserCompleted; }

  InputSource dataSource;
  BufferedInputStream bis; // this must be closed when document is finished parsing
  public SaxDocument(String filename, DefaultHandler handler)
  {
    try
    {
      /* FOR DEBUGGING ONLY .. test for successful load of class
      try
      {
        Logger.getLogger(SaxDocument.class).info("attempting to load SAXParser");
        Class.forName("org.apache.xerces.parsers.SAXParser");
        Logger.getLogger(SaxDocument.class).info("successfully loaded SAXParser");
      }
      catch (ClassNotFoundException ex)
      {
        Logger.getLogger(SaxDocument.class).info("CLASS NOT FOUND");
      }
      */

      this.handler = handler;

      // IMPORTANT (version 3.3.32) .. this was changed to use no parameter, which indicates the default parser
      // should be loaded. This change was necessary to prevent a class not found error from
      // occuring when this routine is entered via an external thread (by calling an applet routine from IE)
      parser = XMLReaderFactory.createXMLReader(); // use default parser to prevent exceptions
    //parser = XMLReaderFactory.createXMLReader("org.apache.xerces.parsers.SAXParser"); // previous method of invocation

      parser.setContentHandler(handler);
      FileInputStream fis = new FileInputStream(filename);
      bis = new BufferedInputStream(fis);
      dataSource = new InputSource(bis);
      //FileReader fr = new FileReader(filename);
      //dataSource = new InputSource(fr);

    }
    catch (Exception ex)
    {
      Logger.getLogger(SaxDocument.class.getName()).log(Level.SEVERE, "SaxDocument() constructor exception", ex);
    }
  }

  public SaxDocument(InputStream is, DefaultHandler handler)
  {
    try
    {
      this.handler = handler;
      parser = XMLReaderFactory.createXMLReader("org.apache.xerces.parsers.SAXParser");
      //ContentHandler customContentHandler = new CustomContentHandler();
      parser.setContentHandler(handler);
//      FileInputStream fis = new FileInputStream(filename);
      //InputStream is = url.openStream();
      bis = new BufferedInputStream(is);
      dataSource = new InputSource(bis);
      //FileReader fr = new FileReader(filename);
      //dataSource = new InputSource(fr);

    }
    catch (Exception ex)
    {
        Logger.getLogger(SaxDocument.class.getName()).log(Level.SEVERE, "SaxDocument() constructor exception", ex);
    }
  }
  //========
  // parse()
  //============================================================================
  public void parse() throws IOException, SAXException
  {
      parser.parse(dataSource);
  }
  //========
  // close()
  //============================================================================
  public void close()
  {
    if (bis != null)
    {
      try
      {
        bis.close();
      }
      catch (IOException ex)
      {
          Logger.getLogger(SaxDocument.class.getName()).log(Level.SEVERE, "SaxDocument::close() IOException", ex);
      }
    }
  }
/* NOTE: .. we must provide a Custom default handler for each table type that we read..
  It should have, as a minimum, the following routines
  class CustomContentHandler extends DefaultHandler
  {
    public void startDocument()
    {
      Logger.getLogger(SaxDocument.class).info("START DOCUMENT SEEN");
    }
    public void startElement(String namespaceURI, String localName, String qualifiedName, Attributes attributes)
    {
      //Logger.getLogger(SaxDocument.class).info("HIT START ELEMENT");
    }
    public void characters(char[] content, int start, int length)
    {
      //Logger.getLogger(SaxDocument.class).info("HIT CHARACTERS routine");
    }
    public void endElement(String namespaceURI, String localName, String qualifiedName)
    {
      //Logger.getLogger(SaxDocument.class).info("HIT END ELEMENT");
    }
    public void endDocument()
    {
      Logger.getLogger(SaxDocument.class).info("END DOCUMENT SEEN");
    }
  }
*/
}
